package com.dawn.module.hospital.controller.app.order;

import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.json.JsonUtils;
import com.dawn.framework.web.core.util.RequestUtils;
import com.dawn.module.hospital.controller.app.base.BaseController;
import com.dawn.module.hospital.controller.app.order.vo.*;
import com.dawn.module.hospital.service.OrderThirdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/19 10:09
 */
@Tag(name = "第三方调用APP - bh")
@RestController
@RequestMapping("/bh/pay")
@Validated
@Slf4j
public class AppOrderThirdController extends BaseController {

    @Autowired
    private OrderThirdService orderThirdService;

    @RequestMapping(value = "/refund")
    @Operation(summary = "退费")
    @PermitAll
    public CommonResult refund(HttpServletRequest request) {
        log.info("---------refund 退费 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------refund 退费参数-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------refund 退费参数-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderRefundReqVO refundReqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json),OrderRefundReqVO.class);
        refundReqVO.setClientIp(RequestUtils.getClientIp(request));
        CommonResult result = orderThirdService.refund(refundReqVO);
        return result;
    }

    @RequestMapping(value = "/refund/chs")
    @Operation(summary = "医保支付退费")
    @PermitAll
    public CommonResult refundChs(HttpServletRequest request) {
        log.info("---------chs refund 医保支付退费 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------chs refund 医保支付退费参数-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------chs refund 医保支付退费参数-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderRefundChsReqVO refundChsReqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json),OrderRefundChsReqVO.class);
        log.warn("---------chs refund 医保支付退费参数-------refundChsReqVO:"+ JsonUtils.toJsonString(refundChsReqVO));
        refundChsReqVO.setClientIp(RequestUtils.getClientIp(request));
        CommonResult result = orderThirdService.refundChs(refundChsReqVO);
        return result;
    }

    @RequestMapping(value = "/getQrURL")
    @Operation(summary = "HIS获取处方二维码")
    @PermitAll
    public CommonResult getQrURL(HttpServletRequest request) {
        log.info("---------his getQrURL HIS获取处方二维码 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------his getQrURL HIS获取处方二维码-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------his getQrURL HIS获取处方二维码-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderThirdQRReqVO orderQRReqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json),OrderThirdQRReqVO.class);
        orderQRReqVO.setClientIp(RequestUtils.getClientIp(request));
        CommonResult result = orderThirdService.getQrURL(orderQRReqVO);
        return result;
    }

    @RequestMapping(value = "/auth-code")
    @Operation(summary = "付款码支付")
    @PermitAll
    public CommonResult authCode(HttpServletRequest request) {
        log.info("---------his authCode 付款码支付 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------his authCode 付款码支付-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------his authCode 付款码支付-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderThirdAuthCodeReqVO reqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json), OrderThirdAuthCodeReqVO.class);
        reqVO.setClientIp(RequestUtils.getClientIp(request));
        CommonResult result = orderThirdService.authCodePay(reqVO);
        return result;
    }

    @PostMapping(value = "/query")
    @Operation(summary = "订单信息查询")
    @PermitAll
    public CommonResult payQuery(HttpServletRequest request) {
        log.info("---------his query 订单信息查询 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------his query 订单信息查询-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------his query 订单信息查询-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderThirdQueryReqVO reqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json), OrderThirdQueryReqVO.class);
        CommonResult result = orderThirdService.payQuery(reqVO);
        return result;
    }

    @PostMapping(value = "/bill/download")
    @Operation(summary = "账单下载")
    @PermitAll
    public CommonResult billDownload(HttpServletRequest request) {
        log.info("---------billDownload 账单下载 start-------");
        JSONObject json = RequestUtils.getReqParamJSON(request);
        log.warn("---------billDownload 账单下载-------JSONObject:"+ json.toJSONString());
        if(json.isEmpty()){
            Map param = parseRequestParamMap(request, false);
            log.warn("---------billDownload 账单下载-------Map:"+ JsonUtils.toJsonString(param));
            json = JsonUtils.parseObject(JsonUtils.toJsonString(param),JSONObject.class);
        }
        OrderThirdBillDownloadReqVO reqVO = JsonUtils.parseObject(JsonUtils.toJsonString(json), OrderThirdBillDownloadReqVO.class);
        CommonResult result = orderThirdService.billDownload(reqVO);
        return result;
    }

}
